import json
import os.path
import time
import requests
from bs4 import BeautifulSoup
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from util import unique_id, insert_hot_words, baidu_search

interval = 600 # 间隔10分钟
source_id = 3
url = "https://news.so.com/hotnews?src=hotnews"
requests.session()
response_data = requests.get(url)
response_data = response_data.content
bs = BeautifulSoup(response_data, "html.parser")
bs4_list = []
hot_div = bs.find("div", id="hot-list")
update_time = hot_div.find('span', attrs={"class":"update-time"}).text
response_data = hot_div.find_all('ul')
for val in response_data:
    content = val.find_all("li")
    for val2 in content:
        bs4_list+=val2.find_all('a')

hot_search = []
for val in bs4_list:
    span = val.find_all("span")
    hot_search.append({
        "title":span[1].text,
        "hot_info":span[2].text,
        "is_increase":bool((val.find("i", attrs={"class":"fire"})))
    })
dir_name = './result/'
if not os.path.isdir(dir_name):
    os.mkdir(dir_name)
time1 = time.localtime(time.time())
file_name = time.strftime('%Y-%m-%d %H-%M-%S', time1)
file_name = f"{dir_name}{file_name}.json"

response_data = {
    "time":time.strftime('%Y-%m-%d %H:%M:%S', time1),
    "update_time":update_time,
    "success":len(hot_search) > 0,
    "data":hot_search,
}

def so_words():
    all_data_tuple = []
    hot_words = []
    params = ("id", "source_id", "title","is_new","is_hot","is_fei", "create_time","hot_index","category","onboard_time","info_complete")
    create_time = str(int(time.time()))
    records = json.loads(json.dumps(response_data["data"]))
    for val3 in records:
        item = {
            "id": int(unique_id()),
            "source_id": source_id,
            "title": val3['title'],
            "is_new":0,
            "is_hot": int(val3['is_increase']),
            "is_fei": 0,
            "create_time": create_time,
            "hot_index":0,
            "category":"",
            "onboard_time":0,
            "info_complete": 0
        }
        hot_words.append(item)
        all_data_tuple.append(tuple(item.values()))
    flag = insert_hot_words(params, hot_words)
    if flag:
        return hot_words
    return []

if response_data["success"] == True and len(response_data["data"]) > 0:
    data_list = so_words()
    if type(data_list) == list and len(data_list) > 0:
        print('360热词搜索结束')
        baidu_search(data_list)
    elif type(data_list) == list and len(data_list) == 0:
        print('360热词搜索为空')
    else:
        print('360搜索失败;未进行百度搜索')

